package com.youlei.newgraduateproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlei.newgraduateproject.common.dto.SysMenuDto;
import com.youlei.newgraduateproject.dao.SysMenuMapper;
import com.youlei.newgraduateproject.dao.SysUserMapper;
import com.youlei.newgraduateproject.entity.SysMenu;
import com.youlei.newgraduateproject.entity.SysUser;
import com.youlei.newgraduateproject.service.SysMenuService;
import com.youlei.newgraduateproject.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author youlei
 * @since 2021-05-08
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        //从security中获取当前用户的用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取当前用户信息
        SysUser sysUser = sysUserService.getByUserName(username);
        //获取菜单信息
        List<Long> menuIds = sysUserMapper.getNavMenuIds(sysUser.getId());
        List<SysMenu> sysMenus = this.listByIds(menuIds);
        //转成树状结构
        List<SysMenu> menuTree = buildTreeMenu(sysMenus);

        //entity(实体)转dto(数据传输对象)
        return convert(menuTree);

    }

    @Override
    public List<SysMenu> tree() {
        // 获取所有菜单信息
        List<SysMenu> sysMenus = this.list(new QueryWrapper<SysMenu>().orderByAsc("orderNum"));

        // 转成树状结构
        return buildTreeMenu(sysMenus);
    }

    /**
     * @Author youlei
     * @Description entity(实体)转dto(数据传输对象)
     * @Date 23:36 2021/5/13
     * @Param: [menuTree]
     * @return: java.util.List<com.youlei.newgraduateproject.common.dto.SysMenuDto>
     **/
    private List<SysMenuDto> convert(List<SysMenu> menuTree) {
        List<SysMenuDto> menuDtos = new ArrayList<>();
        menuTree.forEach(m->{
            SysMenuDto dto = new SysMenuDto();
            dto.setId(m.getId());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setComponent(m.getComponent());
            dto.setPath(m.getPath());
            //若有子节点则将其也转成dto
            if (m.getChildren().size() > 0) {
                // 子节点调用当前方法进行再次转换(递归)
                dto.setChildren(convert(m.getChildren()));
            }
            menuDtos.add(dto);

        });

        return menuDtos;
    }

    private List<SysMenu> buildTreeMenu(List<SysMenu> sysMenus) {
        List<SysMenu> finalMenus = new ArrayList<>();
        //先寻找到各自节点的children
        for(SysMenu sysMenu:sysMenus){
            //将子节点放入父节点的children属性中
            for(SysMenu e:sysMenus){
                if(Objects.equals(sysMenu.getId(), e.getParentId())){
                   sysMenu.getChildren().add(e);
                }

            }
            //获取顶层父节点加入finalMenu
            if(Objects.equals(sysMenu.getParentId(),0L)){
                finalMenus.add(sysMenu);
            }
        }
        return finalMenus;
    }
}
